package log350.tp3;

import classes.User;
import classes.UserHandler;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;

public class CreationCompteActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_creation_compte);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_creation_compte, menu);
        return true;
    }
    public void cancel(View view) {
    	Intent intent = new Intent();
        setResult(RESULT_CANCELED, intent);
        finish();
    }
    
    public String verifierData(String username,String password, String confirm,String gender,String email,String nom,String prenom){
    	String msg = "";
    	
    	if(username.trim().equals(""))
    		msg += "Veuillez entrer un username.\n";
    	if(password.trim().equals(""))
    		msg += "Veuillez entrer un password.\n";
    	if (!password.equals(confirm))
    		msg += "Le password et la confirmation ne sont pas pareil.\n";
    	if(gender == null)
    		msg += "Veuillez entrer un gender.\n";
    	if(email.trim().equals(""))
    		msg += "Veuillez entrer un email.\n";
    	if(nom.trim().equals(""))
    		msg += "Veuillez entrer un nom.\n";
    	if(prenom.trim().equals(""))
    		msg += "Veuillez entrer un prenom.\n";
    	
    	return msg;
    }
    
    public void register(View view) {
    	String username = "";
    	EditText txtusername = ((EditText) findViewById(R.id.txtUsername));
    	username = txtusername.getText().toString();
    	
    	String prenom = "";
    	EditText txtPrenom = ((EditText) findViewById(R.id.txtPrenom));
    	prenom = txtPrenom.getText().toString();
    	
    	String nom = "";
    	EditText txtNom = ((EditText) findViewById(R.id.txtNom));
    	nom = txtNom.getText().toString();
    	
    	String email = "";
    	EditText txtEmail = ((EditText) findViewById(R.id.txtEmail));
    	 email = txtEmail.getText().toString();
    	
    	String password = "";
    	EditText txtPassword = ((EditText) findViewById(R.id.txtPassword));
    	password = txtPassword.getText().toString();
    	
    	String confirm = "";
    	EditText txtConfirm = ((EditText) findViewById(R.id.txtConfirm));
    	confirm = txtConfirm.getText().toString();
    	
    	String gender = "";
    	RadioButton rdbM = ((RadioButton) findViewById(R.id.rdbM));
    	RadioButton rdbF = ((RadioButton) findViewById(R.id.rdbF));
    	if (rdbM.isChecked())
    		gender = "M";
    	else if( rdbF.isChecked())
    		gender = "F";
    	else gender = null;
    	
    	User user = new User(username, password, gender, email, nom, prenom);
    	String error =verifierData(username, password,confirm, gender, email, nom, prenom);
    	if (error.equals("")){
    		UserHandler.getInstance().addUser(user);
    		UserHandler.getInstance().login(user);
    		

            setResult(RESULT_OK, new Intent());
            finish();
    	}
    	else{
    		AlertDialog.Builder alert = new AlertDialog.Builder(this);
    		alert.setTitle("ERREUR");
    		alert.setMessage(error);
    		alert.setPositiveButton("OK", new android.content.DialogInterface.OnClickListener() {                
    	        public void onClick(DialogInterface dialog, int which) {
    	        }
    		 });
    		alert.show();
    	}
    	
    }
    
}
